package com.llianejapan.Quizz;

import java.util.Random;
import java.util.Vector;

import com.llianejapan.LlianeGlobal;
import com.llianejapan.R;
import com.llianejapan.KanjiLists.HIRAGANA;
import com.llianejapan.KanjiLists.KATAKANA;
import com.llianejapan.KanjiLists.KANJIML;
import com.llianejapan.ToRoma.Complex1D;
import com.llianejapan.ToRoma.ComplexDoubleFFT;
import com.llianejapan.Types.KanjiTypeOCR;
import com.llianejapan.Types.MatchScore2;
import com.llianejapan.Types.QuizzType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class QuizzQ extends Activity {
	public TextView Q;
	public Button A0;
	public Button A1;
	public Button A2;
	public Button A3;
	public Button FC;
	public Button D_C_b;
	public Button D_OK_b;
	public ImageButton D_Color_b;
	public Paint D_Paint;
	public ImageView D_View;
	public Canvas D_Cvs;
	public TextView D_Q;
	int oldX;
	int oldY;
	public AlertDialog ad;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(android.R.style.Theme_Black_NoTitleBar);
		load_next(0);
		//TODO : SORTIR
	}
	public void popup_right()
	{
		ad.setTitle(getString(R.string.quizz_right_title));
		ad.setMessage(getString(R.string.quizz_right_text));
		ad.setIcon(android.R.drawable.checkbox_on_background);
		ad.setButton(getString(R.string.quizz_next),
				new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton)
			{
				load_next(1);
			}
		});
		ad.show();
	}
	public void popup_wrong(String wrong, String right)
	{
		ad.setTitle(getString(R.string.quizz_wrong_title));
		ad.setMessage(getString(R.string.quizz_wrong_text_1) + wrong + getString(R.string.quizz_wrong_text_2) + right);
		ad.setIcon(android.R.drawable.checkbox_off_background);
		ad.setButton(getString(R.string.quizz_next),
				new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton)
			{
				load_next(1);
			}
		});
		ad.show();	}
	public void load_next(int incr)
	{
		QuizzL.qact += incr;
		if (QuizzL.qact != QuizzL.q.size())
		{
			final QuizzType myq = QuizzL.q.elementAt(QuizzL.qact);

			//It's a QCM
			if (myq.type.equals("q"))
			{
				setContentView(R.layout.quizz_q);
				Q = (TextView) findViewById(R.id.Q);
				A0 = (Button) findViewById(R.id.A0);
				A1 = (Button) findViewById(R.id.A1);
				A2 = (Button) findViewById(R.id.A2);
				A3 = (Button) findViewById(R.id.A3);
				ad = new AlertDialog.Builder(QuizzQ.this).create();
				A0.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						if (A0.getText().equals(myq.answers[0]))
							popup_right();
						else
							popup_wrong((String) A0.getText(), myq.answers[0]);
					}
				});
				A1.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						if (A1.getText().equals(myq.answers[0]))
							popup_right();
						else
							popup_wrong((String) A1.getText(), myq.answers[0]);
					}
				});
				A2.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						if (A2.getText().equals(myq.answers[0]))
							popup_right();
						else
							popup_wrong((String) A2.getText(), myq.answers[0]);
					}
				});
				A3.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						if (A3.getText().equals(myq.answers[0]))
							popup_right();
						else
							popup_wrong((String) A3.getText(), myq.answers[0]);
					}
				});

				Random rand = new Random();
				int mrand;
				int done[] = {0, 0, 0, 0};
				Q.setText(myq.question);
				while ((done[mrand = rand.nextInt(4)] != 0));
				A0.setText(myq.answers[mrand]);
				done[mrand] = 1;
				while ((done[mrand = rand.nextInt(4)] != 0));
				A1.setText(myq.answers[mrand]);
				done[mrand] = 1;
				while ((done[mrand = rand.nextInt(4)] != 0));
				A2.setText(myq.answers[mrand]);
				done[mrand] = 1;
				while ((done[mrand = rand.nextInt(4)] != 0));
				A3.setText(myq.answers[mrand]);
				done[mrand] = 1;
			}
			else if (myq.type.equals("f"))
			{
				setContentView(R.layout.quizz_f);
				FC = (Button) findViewById(R.id.FC);
				FC.setText(myq.question);
				FC.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						load_next(1);
					}
				});

			}
			else if (myq.type.equals("d"))
			{
				ad = new AlertDialog.Builder(QuizzQ.this).create();
				setContentView(R.layout.quizz_d);
				D_Paint = new Paint();
				D_Paint.setStrokeCap(Paint.Cap.ROUND);
				D_Paint.setStrokeWidth(14);
				D_Paint.setColor(Color.BLACK);
				D_OK_b = (Button) findViewById(R.id.D_OK_b);
				D_C_b = (Button) findViewById(R.id.D_C_b);
				D_Color_b = (ImageButton) findViewById(R.id.D_Color_b);
				D_Color_b.setImageResource(R.drawable.dot_black);
				D_View = (ImageView) findViewById(R.id.D_View);
				D_Q = (TextView) findViewById(R.id.D_Q);
				D_Q.setText(myq.question);
				if (LlianeGlobal.D_Bmp == null)
				{
					LlianeGlobal.quizz_strokes = 0;
					LlianeGlobal.D_Bmp = Bitmap.createBitmap(320, 200, Bitmap.Config.ARGB_8888);
					for (int x = 0; x < 320; x++)
						for (int y = 0; y < 200; y++)
							LlianeGlobal.D_Bmp.setPixel(x, y, Color.WHITE);
				}
				D_View.setImageBitmap(LlianeGlobal.D_Bmp);
				D_View.setOnTouchListener(new OnTouchListener () {
					public boolean onTouch(View v, MotionEvent event) {
						D_Cvs = new Canvas(LlianeGlobal.D_Bmp);				
						if (event.getAction() != MotionEvent.ACTION_DOWN)
							D_Cvs.drawLine(event.getX(), event.getY(), oldX, oldY, D_Paint);
						else
							LlianeGlobal.quizz_strokes++;
						oldX = (int) event.getX();
						oldY = (int) event.getY();
						D_View.setImageBitmap(LlianeGlobal.D_Bmp);
						return true;
					}    	
				});
				D_Color_b.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						if (D_Paint.getColor() == Color.BLACK){
							D_Paint.setColor(Color.WHITE);
							D_Color_b.setImageResource(R.drawable.dot_white);
						}
						else
						{
							D_Paint.setColor(Color.BLACK);
							D_Color_b.setImageResource(R.drawable.dot_black);
						}			
					}    	
				});
				D_OK_b.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						D_match(myq.answers[0]);
					}
				});
				D_C_b.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v){
						LlianeGlobal.quizz_strokes = 0;
						for (int x = 0; x < 320; x++)
							for (int y = 0; y < 200; y++)
								LlianeGlobal.D_Bmp.setPixel(x, y, Color.WHITE);
						D_View.setImageBitmap(LlianeGlobal.D_Bmp);
						oldX = oldY = 0;
					}
				});

			}
		}
		else
		{
			finish();
		}
	}	
	public Vector<MatchScore2> kanjis;
	void D_match(String expected)
	{
		kanjis = new Vector<MatchScore2>();
		int minx = 320;
		int miny = 200;
		int maxx = 0;
		int maxy = 0;
		for (int x = 0; x < 320; x++)
			for (int y = 0; y < 200; y++)
				if (LlianeGlobal.D_Bmp.getPixel(x, y) == Color.BLACK)
				{
					if (x < minx)
						minx = x;
					if (x > maxx)
						maxx = x;
					if (y < miny)
						miny = y;
					if (y > maxy)
						maxy = y;
				}
		minx -= 5;
		miny -= 5;
		maxx += 5;
		maxy +=5;
		if (minx < 0)
			minx = 0;
		if (miny < 0)
			miny = 0;
		if (maxx > 320)
			maxx = 320;
		if (maxy > 200)
			maxy = 200;
		if (minx > maxx)
		{
			minx = 0; maxx = 50;
			miny = 0; maxy = 50;
		}
		Bitmap redim_bmp = Bitmap.createBitmap(LlianeGlobal.D_Bmp,
				minx, miny, maxx - minx, maxy - miny);
		Bitmap scaled_bmp = Bitmap.createScaledBitmap(redim_bmp,
				128, 128, false);
		double score = 0;
		int scorer = 0;
		//Getting the picture pixels
		int pixelsA[] = new int[16384];
		Complex1D pixelsA_C = new Complex1D();
		pixelsA_C.x = new double[16384];
		pixelsA_C.y = new double[16384];
		scaled_bmp.getPixels(pixelsA, 0, 128, 0, 0, 128, 128);
		//Setting all pixels to Color.BLACK || Color.WHITE
		for (int x = 0; x < 16384; x++)
		{
			if (pixelsA[x] != Color.BLACK)	
			{
				pixelsA_C.x[x] = 1;
				pixelsA_C.y[x] = 0;
				pixelsA[x] = Color.WHITE;
			}
			else
			{
				pixelsA_C.x[x] = 0;
				pixelsA_C.y[x] = 0;
			}
		}
		//Computation of FFT
		ComplexDoubleFFT fftpa = new ComplexDoubleFFT(128*128);
		fftpa.ft(pixelsA_C);;
		double[][] fftk = new double[10][2];
		for (int fftn = 0; fftn < 10; fftn++)
		{
			fftk[fftn][0] = pixelsA_C.x[fftn] * 10000;
			fftk[fftn][0] = Math.floor(fftk[fftn][0] + 0.5);
			fftk[fftn][0] = fftk[fftn][0] / 10000;
			fftk[fftn][1] = pixelsA_C.y[fftn] * 10000;
			fftk[fftn][1] = Math.floor(fftk[fftn][1] + 0.5);
			fftk[fftn][1] = fftk[fftn][1] / 10000;
		}	
		//Debut des appels différenciés
		for (int kn = 0; kn < KANJIML.kmlo.length; kn++)
		{	
			match_list(KANJIML.kmlo[kn], fftk);
		}
		match_list(KATAKANA.kl, fftk);
		match_list(HIRAGANA.kl, fftk);
		score = -1;
		scorer = -1;
		for (int j = 0; j < kanjis.size(); j++)
		{
			if ((kanjis.elementAt(j).score_ >= 0) && 
					((kanjis.elementAt(j).score_ <= score) || (score == -1)))
			{
				score = kanjis.elementAt(j).score_;
				scorer = j;
			}
		}
		LlianeGlobal.quizz_strokes = 0;
		for (int x = 0; x < 320; x++)
			for (int y = 0; y < 200; y++)
				LlianeGlobal.D_Bmp.setPixel(x, y, Color.WHITE);
		if (kanjis.elementAt(scorer).kanji_.equals(expected))
			popup_right();
		else
			popup_wrong(kanjis.elementAt(scorer).kanji_, expected);
	}
	public void match_list(final KanjiTypeOCR[] klist, double[][] fftk)
	{
		int score;
		for (int i = 0; i < klist.length; i++)
		{		
			if (LlianeGlobal.option_strokes.isChecked()
					&& (LlianeGlobal.quizz_strokes != 0)
					&& (LlianeGlobal.quizz_strokes != klist[i].strokes_))
				continue;
			score = 0;
			for (int fftn = 0; fftn < 10; fftn++)
			{
				double x_re = klist[i].fftv_[fftn].re();
				double x_im = klist[i].fftv_[fftn].im();
				double y_re = fftk[fftn][0];
				double y_im = fftk[fftn][1];
				double re_ = x_re - y_re;
				double im_ = x_im - y_im;
				score += im_ * im_ + re_ * re_;
			}
			kanjis.add(new MatchScore2(klist[i].kanji_, score));
		}		
	}
}

